package framework.transport.codec;

import lombok.Data;
import framework.enums.XResponseCodeEnum;

import java.io.Serializable;

@Data
public class XResponse<T> implements Serializable {

    private static final long serialVersionUID = 715745410605631233L;
    private String requestId;
    /**
     * response code
     */
    private Integer code;
    /**
     * response message
     */
    private String message;
    /**
     * response body
     */
    private T data;

    public static <T> XResponse<T> success(T data, String requestId) {
        XResponse<T> response = new XResponse<>();
        response.setCode(XResponseCodeEnum.SUCCESS.getCode());
        response.setMessage(XResponseCodeEnum.SUCCESS.getMessage());
        response.setRequestId(requestId);
        if (null != data) {
            response.setData(data);
        }
        return response;
    }

    public static <T> XResponse<T> fail(XResponseCodeEnum XResponseCodeEnum, String requestId, Exception exception) {
        XResponse<T> response = new XResponse<>();
        response.setRequestId(requestId);
        response.setCode(XResponseCodeEnum.getCode());
        response.setMessage(XResponseCodeEnum.getMessage());
        response.data = (T) exception;
        return response;
    }

    public static <T> XResponse<T> fail(String message, String requestId) {
        XResponse<T> response = new XResponse<>();
        response.setRequestId(requestId);
        response.setCode(500);
        response.setMessage(message);
        return response;
    }
}
